package com.yricky.atri.activity

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.ViewModelProvider
import com.google.gson.Gson
import com.yricky.atri.viewmodel.MainActivityViewModel

class MainActivity : AppCompatActivity() {
    private val viewModel:MainActivityViewModel by lazy {
        ViewModelProvider(this)[MainActivityViewModel::class.java]
    }

    private val addModelLauncher = registerForActivityResult(ActivityResultContracts.OpenDocument()){
        it?.let{ uri->
            viewModel.addModel(
                "${DocumentFile.fromSingleUri(this,uri)?.name}",
                contentResolver.openInputStream(uri)?:return@let
            )
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val viewState by viewModel.viewState.collectAsState()
            MaterialTheme {
                Scaffold {
                    LazyColumn(
                        Modifier
                            .padding(it)
                            .fillMaxSize()){
                        items(viewState.modelList){ pair->
                            Column(
                                Modifier
                                    .clickable {
                                        startActivity(
                                            Intent(this@MainActivity,CameraActivity::class.java).apply{
                                                putExtra("path",pair.first.path)
                                            }
                                        )
                                    }
                                    .padding(8.dp)
                                    .fillMaxWidth()) {
                                Text(text = pair.second.name, fontSize = 20.sp)
                                Text(text = Gson().toJson(pair.second))
                            }
                        }
                    }
                }
            }

        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menu.add(0,0,0,"Add")
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId == 0){
            addModelLauncher.launch(arrayOf("*/*"))
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onResume() {
        super.onResume()
        viewModel.listModels()
    }
}